<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 16/4/18
 * Time: 上午10:37
 * Describe:
 */

namespace services\models;
use Yii;

class ProhibitedWords extends \yii\base\Object
{
    public $_words;
    public $words;
    public function init(){
        $this->_words = require Yii::getAlias('@common').'/config/words.php';
        parent::init();

    }

    public function filterWords($content){
        if(empty($this->words)){
            $this->words = array_combine($this->_words,array_fill(0,count($this->_words),'***'));
        }

        return strtr($content, $this->words);

    }

    public function filterForComments($content){
        $msg = ['flag'=>true];
        foreach( $this->_words as $value) {
            if(strstr($content,trim($value))) {

                $msg = ['flag'=>false,'msg'=>mb_substr(trim($value),0,1,'utf8')."***"];
                break;
            }
        }

        return $msg;

    }


}